package com.lib.core.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import com.lib.core.R;


/**
 * 标题栏
 */
public class NavTitleBar extends RelativeLayout {

    private final Context context;
    public TextView tvTitle;
    public TextView tvRight;
    AppCompatImageView ivCenterImage;
    public AppCompatImageButton ivLeftImageView;
    public AppCompatImageButton ivRightImageView;
    RelativeLayout rootView;

    private String titleText;
    private int textTitleColor;
    private int titleTextStyle;
    private float titleTextSize;
    private int centerImage;
    private int leftImage;
    private int rightImage;
    private boolean showLeftImage;
    private int backgroundColor;
    private int titleRightTextColor;
    private int titleRightTextSize;
    private String rightText;
    private int background;
    private View contentView;


    public NavTitleBar(Context context) {
        this(context, null);
    }

    public NavTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("ResourceAsColor")
    public NavTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initAttr(context, attrs, defStyleAttr);
        findView();
        initTitle(context);
    }

    private void findView() {
        contentView = LayoutInflater.from(getContext()).inflate(R.layout.nav_title_bar, this, false);
        tvTitle = contentView.findViewById(R.id.tv_title);
        tvRight = contentView.findViewById(R.id.tv_right);
        ivCenterImage = contentView.findViewById(R.id.iv_centerImage);
        ivLeftImageView = contentView.findViewById(R.id.iv_left);
        ivRightImageView = contentView.findViewById(R.id.iv_right);
        rootView = contentView.findViewById(R.id.rootView);
        addView(contentView);
        ivLeftImageView.setOnClickListener(v -> onClickFinish());
    }

    private void initAttr(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavTitleBar, defStyleAttr, 0);
        titleText = a.getString(R.styleable.NavTitleBar_titleText);
        rightText = a.getString(R.styleable.NavTitleBar_rightText);
        textTitleColor = a.getColor(R.styleable.NavTitleBar_titleTextColor, Color.parseColor("#1A1A1A"));
        titleRightTextColor = a.getColor(R.styleable.NavTitleBar_rightTextColor, Color.parseColor("#1A1A1A"));
        titleTextStyle = a.getInt(R.styleable.NavTitleBar_titleText_Style, 0);
        titleTextSize = a.getDimensionPixelSize(R.styleable.NavTitleBar_titleTextSize, getResources().getDimensionPixelSize(R.dimen.dp18));
        titleRightTextSize = a.getDimensionPixelSize(R.styleable.NavTitleBar_rightTextSize, getResources().getDimensionPixelSize(R.dimen.dp13));
        centerImage = a.getResourceId(R.styleable.NavTitleBar_centerImage, 0);
        leftImage = a.getResourceId(R.styleable.NavTitleBar_leftImage, R.mipmap.icon_black_back);
        rightImage = a.getResourceId(R.styleable.NavTitleBar_rightImage, 0);
        background = a.getResourceId(R.styleable.NavTitleBar_nav_background, 0);
        showLeftImage = a.getBoolean(R.styleable.NavTitleBar_showLeftImage, true);
        backgroundColor = a.getColor(R.styleable.NavTitleBar_nav_backgroundColor, ContextCompat.getColor(context, android.R.color.white));
        a.recycle();
    }

    private void initTitle(Context context) {
        if (background == 0) {
            rootView.setBackgroundColor(backgroundColor);
        } else {
            rootView.setBackground(ContextCompat.getDrawable(context,background));
        }
        tvTitle.setText(titleText);
        tvTitle.setTextColor(textTitleColor);
        tvTitle.setTypeface(Typeface.defaultFromStyle(titleTextStyle == 1 ? Typeface.BOLD : Typeface.NORMAL));
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        ivCenterImage.setImageResource(centerImage);
        ivLeftImageView.setImageResource(leftImage);
        ivLeftImageView.setVisibility(showLeftImage ? VISIBLE : GONE);
        ivRightImageView.setImageResource(rightImage);
        tvRight.setTextColor(titleRightTextColor);
        tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleRightTextSize);
        tvRight.setText(rightText);
        if (!TextUtils.isEmpty(rightText))
            ivRightImageView.setVisibility(GONE);
    }





    public void setTitleRightText(String text) {
        tvRight.setText(TextUtils.isEmpty(text) ? "" : text);
        tvRight.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
    }

    public void setTitleRightText(@StringRes int textRes) {
        tvRight.setText(textRes);
        tvRight.setVisibility(VISIBLE);
    }

    public void setTitleRightText(@StringRes int textRes, Intent intent) {
        setTitleRightText(textRes);
        tvRight.setOnClickListener(v -> context.startActivity(intent));
    }


    public void setTitleRightText(String textRes, Intent intent) {
        setTitleRightText(textRes);
        tvRight.setOnClickListener(v -> context.startActivity(intent));
    }





    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setTitle(@StringRes int title) {
        tvTitle.setText(title);
    }




    void onClickFinish() {
        if (context instanceof Activity)
            ((Activity) context).finish();
    }

}
